package com.qf.factory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @Description: 公用常量
 * @Data: 2022/8/27 17:43
 * @Pacakge: com.qf.factory
 * @ClassName: BeanFactory
 * @Author: fan
 * 解耦工具类1.0
 * 通过工具类,反射,创建Bean实体
 */
public class BeanFactory {
    private static Properties properties;
    static{
        try {
        //获取输入流
        InputStream is = BeanFactory.class.getClassLoader().getResourceAsStream("db.properties");
        //创建Properties对象,用于获取配置文件中的信息
          properties = new Properties();
        //加载输入流
            properties.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //获取Bean实体类对象的功能
    public static Object getBean(String name){
        String value = properties.getProperty(name);
        try {
            //通过反射获取实体类对象
            Object o = Class.forName(value).newInstance();
            return o;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
